package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberAuditBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberAuditBean;
import com.sgb.member.ui.adapter.MemberAuditAdapter;
import com.sgb.member.view_model.CustomerApplicationModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by：冯齐 on 2020/12/31 15:57
 * Description：用户中心-会员审核-会员申请
 */
public class CustomerApplicationFragment extends AppLazyFragment implements MemberStatusView.OnChooseListener {
    private FragmentMemberAuditBinding binding;
    @Inject
    CustomerApplicationModel viewModule;
    private MemberAuditAdapter mAdapter;

    private String mType = ""; // 审核状态
    private String mStatusNo = ""; // 审核状态
    private String mMainNo = "";// 会员主体:1 个人 2 企业 默认为企业会员

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    public static CustomerApplicationFragment newInstance() {
        CustomerApplicationFragment fragment = new CustomerApplicationFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_audit, container, false);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
    }

    private void loadData(boolean isLoadMore) {
        viewModule.getCheckMember(isLoadMore, mType, mMainNo, mStatusNo); // 1 会员申请  2企业邀请
    }

    @Override
    protected void initView() {
        LoadingMoreFooter footer = (LoadingMoreFooter) binding.xrv.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.xrv.setLayoutManager(layoutManager);
        initAuditAdapter();
        binding.xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });

        binding.auditType.initPopup(binding.grayLayout, binding.popLine, this);
        binding.auditStatus.initPopup(binding.grayLayout, binding.popLine, this);
        binding.auditMain.initPopup(binding.grayLayout, binding.popLine, this);

    }

    private void initAuditAdapter() {
        if (mAdapter == null) {
            mAdapter = new MemberAuditAdapter(getActivity(), null);
        }
        binding.xrv.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<MemberAuditBean>() {
            @Override
            public void onItemClick(MemberAuditBean memberBean, int position) {
                //跳转到H5界面
                if (memberBean.getMemberType() == 1) {
                    String enrUrl = "?no=" + memberBean.getNo() + "&companyNo=" + memberBean.getBusinessNo() + "&joinWay=" + memberBean.getJoinWay();
                    if (memberBean.getStatus() == 1) {
                        enrUrl += "&isExamine=1"; // isExamine从会员审核列表进入传1
                    }
                    viewModule.getUrlSetting("companyExamine", enrUrl);
                } else {
                    //跳转到H5界面
                    String enrUrl = "?no=" + memberBean.getNo() + "&userNo=" + memberBean.getBusinessNo() + "&joinWay=" + memberBean.getJoinWay();
                    if (memberBean.getStatus() == 1) {
                        enrUrl += "&isExamine=1"; // isExamine从会员审核列表进入传1
                    }
                    viewModule.getUrlSetting("personalExamine", enrUrl);
                }
            }
        });
    }

    public void showEmpty() {
        mAdapter.clearDatas();
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<MemberAuditBean> list, boolean isLoadMore, boolean isLastPage) {
        binding.xrv.setVisibility(View.VISIBLE);
        if (isLoadMore) {
            binding.xrv.loadMoreComplete();
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
            binding.xrv.setNoMore(isLastPage);
        } else {
            binding.xrv.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "审核状态":
                viewModule.getInviteOrApplyStatus();
                break;
            case "会员主体":
                viewModule.getMemberMainType();
                break;
            case "审核类型":
                viewModule.getAuditType();
                break;
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        switch (mKeyName) {
            case "审核状态":
                if (!TextUtils.equals(mStatusNo, entity.getId())) {
                    mStatusNo = entity.getId();
                    loadData(false);
                }
                break;
            case "会员主体":
                if (!TextUtils.equals(mMainNo, entity.getNo())) {
                    mMainNo = entity.getNo();
                    loadData(false);
                }
                break;
            case "审核类型":
                if (!TextUtils.equals(mType, entity.getNo())) {
                    mType = entity.getNo();
                    loadData(false);
                }
                break;
        }
    }

    public void setInviteOrApplyStatus(List<CommonPopEntity> data) {
        binding.auditStatus.setData(data);
        binding.auditStatus.showPop();
    }

    public void setMemberMainType(List<CommonPopEntity> listMain) {
        binding.auditMain.setData(listMain);
        binding.auditMain.showPop();
    }

    public void setAuditType(List<CommonPopEntity> listMain) {
        binding.auditType.setData(listMain);
        binding.auditType.showPop();
    }
}
